﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.下单MTO
{
    public partial class FormMTO客户套号分录 : DevExpress.XtraEditors.XtraForm
    {
        public List<String> details { get; set; }
        private List<SaleOrderQueryInfoDTO> SaleOrderQueryInfoDTOs = new List<SaleOrderQueryInfoDTO>();

        private Dictionary<String, List<OrderCommitDTO返回>> detailIdAndOrders = new Dictionary<string, List<OrderCommitDTO返回>>();

        private Dictionary<String, List<String>> detailIdAndBarCodes = new Dictionary<string, List<String>>();

        private String detailId;

        public FormMTO客户套号分录(List<String> details)
        {
            InitializeComponent();
            this.details = details;
            this.refreshData(details);
        }
        public FormMTO客户套号分录()
        {
            InitializeComponent();
        }
        public void refreshData(List<String> details)
        {
            this.details = details;
            try
            {
                List<SaleOrderQueryDTO> saleOrderQueryDTOs = MTO下单Service.getSaleOrderQueryListByDetailIds(details);
                this.SaleOrderQueryInfoDTOs.Clear();
                foreach (SaleOrderQueryDTO saleOrderQueryDTO in saleOrderQueryDTOs)
                {
                    //是一个List，用add添加单个对象。传的参就是上一个dto
                    this.SaleOrderQueryInfoDTOs.Add(new SaleOrderQueryInfoDTO(saleOrderQueryDTO));
                }
            }
            catch
            {

            }
            this.gridControl客户套号分录.DataSource = this.SaleOrderQueryInfoDTOs;
            //this.advBandedGridView销售订单一览.RefreshData();
        }

        private void gridView客户套号分录_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            this.detailId = FunctionHelper.GetgridViewValue(this.gridView客户套号分录, e.RowHandle, "id");
            List < OrderCommitDTO返回 > orderCommitDTOs = MTO下单Service.getAccomplistOrderBySaleDetailId(this.detailId);
            if (!this.detailIdAndOrders.ContainsKey(this.detailId))
            {
                //  没有点击
                this.detailIdAndOrders.Add(this.detailId, orderCommitDTOs);
            }
            else {
                foreach (OrderCommitDTO返回 orderCommitDTO in orderCommitDTOs) {
                    bool match = false;
                    foreach (OrderCommitDTO返回 orderCommitDTO一览 in this.detailIdAndOrders[this.detailId]) {
                        if (FunctionHelper.JsonSerialization(orderCommitDTO).Equals(FunctionHelper.JsonSerialization(orderCommitDTO一览))) {
                            match = true;
                            break;
                        }
                    }
                    if(!match)
                        this.detailIdAndOrders[this.detailId].Add(orderCommitDTO);
                }
            }

            this.gridControl已完成一览.DataSource = this.detailIdAndOrders[this.detailId];
            this.gridView已完成一览.RefreshData();
        }

        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.detailId))
                {
                    //  报错，没有选中销售分录
                    MessageBox.Show("请选择销售分录");
                }
                else {
                    String barcode = this.textBox扫码.Text;
                    OrderCommitDTO返回 orderCommitDTO = MTO订单Service.getOrderCommitDtoByBarcode(barcode);
                    if (String.IsNullOrEmpty(orderCommitDTO.orderId))
                    {
                        //  报错，吊牌码错误
                        MessageBox.Show("吊牌输入错误");
                    }
                    else {
                        if (this.detailIdAndBarCodes.ContainsKey(this.detailId))
                        {
                            if (!this.detailIdAndBarCodes[this.detailId].Contains(barcode))
                            {
                                this.detailIdAndBarCodes[this.detailId].Add(barcode);

                                //  没有重复扫描
                                //  显示刷新
                                this.detailIdAndOrders[this.detailId].Add(orderCommitDTO);
                            }
                            else {
                                //  报错，重复扫描
                                MessageBox.Show("已重复扫描");
                            }
                        }
                        else {
                            List<String> barcodes = new List<string>();
                            barcodes.Add(barcode);
                            this.detailIdAndBarCodes.Add(this.detailId, barcodes);
                            if (this.detailIdAndOrders.ContainsKey(this.detailId))
                            {
                                this.detailIdAndOrders[this.detailId].Add(orderCommitDTO);
                            }
                            else {
                                List<OrderCommitDTO返回> orderCommitDTOs = new List<OrderCommitDTO返回>();
                                orderCommitDTOs.Add(orderCommitDTO);
                                this.detailIdAndOrders.Add(this.detailId, orderCommitDTOs);
                            }
                        }

                        this.gridControl已完成一览.DataSource = this.detailIdAndOrders[this.detailId];
                        this.gridView已完成一览.RefreshData();
                    }
                }

            }
        }

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            MTO下单Service.mtoWIPDoneByInventory(this.detailIdAndBarCodes);
        }
    }
}
